package com.zzty.serviceimpl;

import com.zzty.common.Split;
import com.zzty.dao.GroupDao;
import com.zzty.dao.QuestionDao;
import com.zzty.dao.TeamExamDao;
import com.zzty.dao.TeamExamTransactionDao;
import com.zzty.daoimpl.GroupDaoImpl;
import com.zzty.daoimpl.QuestionDaoImpl;
import com.zzty.daoimpl.TeamExamDaoImpl;
import com.zzty.daoimpl.TeamExamTransactionDaoImpl;
import com.zzty.entity.Groups;
import com.zzty.entity.HistoryGroup;
import com.zzty.entity.Question;
import com.zzty.entity.TeamExamTransaction;
import com.zzty.service.TeamExamTransactionService;


import java.util.*;

public class TeamExamTransactionServiceImpl implements TeamExamTransactionService {
    TeamExamDao ted = new TeamExamDaoImpl();
    GroupDao gd = new GroupDaoImpl();
    TeamExamTransactionDao tetd = new TeamExamTransactionDaoImpl();
    QuestionDao qd=new QuestionDaoImpl();
    Split split=new Split();

    public boolean updateTeamExamTransactionByUserId(TeamExamTransaction teamExamTransaction,Calendar cal){
        boolean result=false;
        result=tetd.updateTeamExamTransactionByUserId(teamExamTransaction,cal);
        return result;
    }
    public List getUrlNamesByTeamExamId(Integer teamExamId){
        List<String> aUrls=new ArrayList<String>();
        List<String> urlNames=new ArrayList<String>();
        if(tetd.getAnsewrUrlByTeamExamId(teamExamId)!=null){
            aUrls=tetd.getAnsewrUrlByTeamExamId(teamExamId);
            for(String aUrl:aUrls){
                String a=aUrl.substring(aUrl.lastIndexOf("\\")+1,aUrl.length());
                urlNames.add(a);
                System.out.println("a="+a);
            }
        }
        return urlNames;
    }
    public boolean setBeginTimeAndUserId(String startTime,String date,Integer groupId){
        boolean result=false;
        Integer teamExamId=ted.getIdByDate(date);
        if(teamExamId!=null) {
            result = tetd.setBeginTimeAndUserId(startTime,teamExamId,groupId);
        }
        return result;
    }
    public List getGroupsByDate(String date){
        Integer id=null;
        List<Groups> groups=new ArrayList<Groups>();
        if(ted.getIdByDate(date)!=null) {
            id = ted.getIdByDate(date);
        }
        System.out.println("id="+id);
        if(tetd.getGroupsByTeamExamId(id)!=null){
            groups=tetd.getGroupsByTeamExamId(id);
        }
        for(Groups gps:groups) {
            if (gd.getUserNamesById(gps.getGroupId())!= null) {
                System.out.println("groupId="+gps.getGroupId());
                System.out.println("usernames="+gd.getUserNamesById(gps.getGroupId()));
                gps.setUserNames(gd.getUserNamesById(gps.getGroupId()));
            }
        }
        return groups;
    }
    public String getUrl(String date,Integer groupId){
        String url=null;
        Integer teamExamId =null;
        if(ted.getIdByDate(date)!=null) {
            teamExamId = ted.getIdByDate(date);
        }
        if(tetd.getUrl(teamExamId,groupId)!=null){
            url=tetd.getUrl(teamExamId,groupId);
        }
        return url;
    }
    public boolean setScore(String date,Integer groupId,Integer score){
        boolean result=false;
        Integer teamExamId=ted.getIdByDate(date);
        if(teamExamId!=null){
            result=tetd.setScore(teamExamId,groupId,score);
        }
        return result;
    }

    @Override
    public List getHistoryGroupByGroupId(Integer groupId) {
        List<HistoryGroup> historyGroups = null;
        List<Integer> teamExamIds = tetd.getTeamExamIdByGroupId(groupId);
        if (teamExamIds != null) {
            for (int i = 0; i < teamExamIds.size(); i++) {
                HistoryGroup historyGroup=new HistoryGroup();
                Integer teamExamId=teamExamIds.get(i);
                Date date=ted.getDateById(teamExamId);
                String questionIds=ted.getQuestionIdById(teamExamId);
                List<Integer> ques=split.getSplit(questionIds);
                String content ="";
                for(Integer q:ques) {
                    content += qd.getContentById(q)+"\n";
                }
                Double score=tetd.getScoreByGroupIdByTeamExamId(teamExamId,groupId);

                historyGroup.setDate(date);
                historyGroup.setQuestion(content);
                historyGroup.setScore(score);
                if(historyGroups==null){
                    historyGroups=new ArrayList<HistoryGroup>();
                }
                historyGroups.add(historyGroup);
            }
        }
        return historyGroups;
    }

    @Override
    public boolean checkToday(String date, Integer groupId) {
        boolean result=false;
        Integer teamExamId=ted.getIdByDate(date);
        if(teamExamId!=null){
            result=tetd.checkToday(teamExamId,groupId);
        }
        return result;
    }
    public boolean checkTodaySubmit(String date, Integer groupId){
        boolean result=false;
        Integer teamExamId=ted.getIdByDate(date);
        if(teamExamId!=null){
            result=tetd.checkTodaySubmit(teamExamId,groupId);
        }
        return result;
    }
    public String getBeginTime(String date, Integer groupId){
        String beginTime=null;
        Integer teamExamId=ted.getIdByDate(date);
        if(teamExamId!=null){
            beginTime=tetd.getBeginTime(teamExamId,groupId);
        }
        return  beginTime;
    }


}
